package com.wzt.lianfirecontrol.fragment.xiaokongshi;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.ShowFragActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.adapter.xiaokongshi.ChooseUserAdapter;
import com.wzt.lianfirecontrol.adapter.xiaokongshi.RecordItemAdapter;
import com.wzt.lianfirecontrol.adapter.xiaokongshi.XksPersonAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.AddXksRecordModel;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksDutyItemModel;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordItemModel;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordModel;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordUserModel;
import com.wzt.lianfirecontrol.bean.recode.user.RoleConstant;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;
import com.wzt.lianfirecontrol.utils.DialogUtils;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.MinioUtils;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import org.json.JSONArray;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import static com.wzt.lianfirecontrol.http.json.ParseJsonData.REQUEST_OK;

/**
 * 待值班 消控室值班检查详情
 */
public class AddDutyDetailFragment extends BaseFragment implements RecordItemAdapter.RecordItemCallBack, XksPersonAdapter.PersonCallBack {
    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    /**
     * 值班人员
     */
    private TextView tv_person_name;
    private TextView tv_person_phone;
    private ImageView iv_add_person;
    private RecyclerView rlv_person_list;
    private XksPersonAdapter xksPersonAdapter;
    private List<UserInfoModel> chooseUserList = new ArrayList<>();

    private RelativeLayout rl_up_sign;
    private ImageView iv_up_sign;
    private ImageView iv_up_sign_delete;
    private String signImgUrl;

    /**
     * 设备运行图片
     */
    private View include_play_photo_three;
    private RelativeLayout rl_device_photo_1;
    private ImageView iv_device_photo_1;
    private ImageView iv_photo_delete_1;
    private RelativeLayout rl_device_photo_2;
    private ImageView iv_device_photo_2;
    private ImageView iv_photo_delete_2;
    private RelativeLayout rl_device_photo_3;
    private ImageView iv_device_photo_3;
    private ImageView iv_photo_delete_3;

    private LinearLayout ll_record_item;
    private RecyclerView rlv_record_item_list;
    private RecordItemAdapter recordItemAdapter;

    private Button bt_submit;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private static final int SUBMIT_DETAIL_WHAT = 0;
    private static final int RECORD_ITEM_WHAT = 3;
    private static final int XKS_PERSON_WHAT = 4;

    private Dialog chooseDialog;

    private String[] imgUrl = new String[]{"", "", ""};
    private boolean isUpdateImg = false;
    private boolean isSubmitSuccess = false;

    private XksRecordModel xksRecordModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            if (null != bundle){
                xksRecordModel = (XksRecordModel) bundle.getSerializable("XksRecordModel");
            }
            if (null == xksRecordModel){
                ToastUtils.showToast(activity, "数据错误");
                activity.finish();
            }
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_add_duty, container, false);
            initView();
            initRecordItemHttpHelper();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {

        initZhiBanPerson();

        initPhotoThree();

        initRecordItemList();

        bt_submit = itemContentView.findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserInfoModel.isSelfCompany(activity)){
               //     ToastUtils.showToast(activity, ConstData.ERROR_COMPANY_MSG);
                    return;
                }
                UserInfoModel userInfoModel = UserInfoModel.getUserInfo(activity);
                if (userInfoModel.getRoleAlias().contains(RoleConstant.ROLE_DUTY)) {
                    //提交数据
                    initHttpHelper();
                }else {
                    ToastUtils.showToast(activity, "非值班员用户");
                }
            }
        });

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        include_progress_bar.setVisibility(View.GONE);
        InitView.instance().initProgressBar(activity, include_progress_bar);

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_no_data.setVisibility(View.GONE);

        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setVisibility(View.GONE);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    private void initZhiBanPerson() {
        chooseUserList.clear();

        tv_person_name = itemContentView.findViewById(R.id.tv_person_name);
        tv_person_phone = itemContentView.findViewById(R.id.tv_person_phone);
        UserInfoModel userInfoModel = UserInfoModel.getUserInfo(activity);
        tv_person_name.setText(userInfoModel.getRealName());
        tv_person_phone.setText(userInfoModel.getPhone());
        // 添加默认数据
        chooseUserList.add(userInfoModel);
        iv_add_person = itemContentView.findViewById(R.id.iv_add_person);
        iv_add_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 请求用户数据
                initPersonHttpHelper();
            }
        });

        rlv_person_list = itemContentView.findViewById(R.id.rlv_person_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rlv_person_list.setLayoutManager(linearLayoutManager);
        xksPersonAdapter = new XksPersonAdapter(activity);
        xksPersonAdapter.setCallBack(this);
        rlv_person_list.setAdapter(xksPersonAdapter);

        rl_up_sign = itemContentView.findViewById(R.id.rl_up_sign);
        iv_up_sign = itemContentView.findViewById(R.id.iv_up_sign);
        iv_up_sign_delete = itemContentView.findViewById(R.id.iv_up_sign_delete);
        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_up_sign.getLayoutParams().width = width;
        rl_up_sign.getLayoutParams().height = width;

        iv_up_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(signImgUrl)){
                    addImageOrPlayPhoto(ConstData.PHOTO_USER_SIGN, true);
                }else {
                    ((ShowFragActivity)activity).setShowImg(signImgUrl);
                }
            }
        });

        iv_up_sign_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, signImgUrl, handler);
                signImgUrl = "";
                iv_up_sign.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_up_sign_delete.setVisibility(View.GONE);
            }
        });
    }

    private void initPhotoThree() {
        include_play_photo_three = itemContentView.findViewById(R.id.include_play_photo_three_1);
        rl_device_photo_1 = include_play_photo_three.findViewById(R.id.rl_device_photo_1);
        iv_device_photo_1 = include_play_photo_three.findViewById(R.id.iv_device_photo_1);
        iv_photo_delete_1 = include_play_photo_three.findViewById(R.id.iv_photo_delete_1);
        rl_device_photo_2 = include_play_photo_three.findViewById(R.id.rl_device_photo_2);
        iv_device_photo_2 = include_play_photo_three.findViewById(R.id.iv_device_photo_2);
        iv_photo_delete_2 = include_play_photo_three.findViewById(R.id.iv_photo_delete_2);
        rl_device_photo_3 = include_play_photo_three.findViewById(R.id.rl_device_photo_3);
        iv_device_photo_3 = include_play_photo_three.findViewById(R.id.iv_device_photo_3);
        iv_photo_delete_3 = include_play_photo_three.findViewById(R.id.iv_photo_delete_3);

        int width = (ScreenUtils.getScreenWidth(activity) - Utils.dip2px(activity, 90)) / 3;
        rl_device_photo_1.getLayoutParams().width = width;
        rl_device_photo_1.getLayoutParams().height = width;
        rl_device_photo_2.getLayoutParams().width = width;
        rl_device_photo_2.getLayoutParams().height = width;
        rl_device_photo_3.getLayoutParams().width = width;
        rl_device_photo_3.getLayoutParams().height = width;

        iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[0])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_1, true);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[0]);
                }
            }
        });
        iv_photo_delete_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[0], handler);
                imgUrl[0] = "";
                updateImageView(ConstData.IMG_TYPE_STATUS);
            }
        });

        iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[1])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_2, true);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[1]);
                }
            }
        });
        iv_photo_delete_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[1], handler);
                imgUrl[1] = "";
                updateImageView(ConstData.IMG_TYPE_STATUS);
            }
        });

        iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                if (StringUtils.isEmpty(imgUrl[2])){
                    addImageOrPlayPhoto(ConstData.PHOTO_DEVICE_LIST_3, true);
                }else {
                    ((ShowFragActivity)activity).setShowImg(imgUrl[2]);
                }
            }
        });
        iv_photo_delete_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isUpdateImg){
                    ToastUtils.showToast(activity, "正在上传图片");
                    return;
                }
                MinioUtils.removeFile(activity, imgUrl[2], handler);
                imgUrl[2] = "";
                updateImageView(ConstData.IMG_TYPE_STATUS);
            }
        });
        updateImageView(ConstData.IMG_TYPE_STATUS);
    }

    private void initRecordItemList() {
        ll_record_item = itemContentView.findViewById(R.id.ll_record_item);
        rlv_record_item_list = itemContentView.findViewById(R.id.rlv_record_item_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rlv_record_item_list.setLayoutManager(linearLayoutManager);
        recordItemAdapter = new RecordItemAdapter(activity);
        recordItemAdapter.setHandler(handler);
        recordItemAdapter.setCallBack(this);
        rlv_record_item_list.setAdapter(recordItemAdapter);

        ll_record_item.setVisibility(View.GONE);
    }

    /**
     * 值班人员请求
     */
    private void initPersonHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", UserInfoModel.getCompandId(activity));

        HashMap<String, String> url_params = new HashMap<>();
        url_params.put("jsonObject", JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XKS_RERSON_URL, XKS_PERSON_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
        include_progress_bar.setVisibility(View.VISIBLE);
    }

    /**
     * 检查项请求
     */
    private void initRecordItemHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", xksRecordModel.getControlRoomId());

        HashMap<String, String> url_params = new HashMap<>();
        url_params.put("jsonObject", JSONUtil.mapToJSonStr(hashMap));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XKS_RECORD_ITEM_URL, RECORD_ITEM_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
        include_progress_bar.setVisibility(View.VISIBLE);
    }

    /**
     * 提交
     */
    private void initHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }

        AddXksRecordModel addXksRecordModel = new AddXksRecordModel();
        addXksRecordModel.setId(xksRecordModel.getId());
        if (StringUtils.isEmpty(signImgUrl)){
            ToastUtils.showToast(activity, "未上传签名图片");
            return;
        }else {
            addXksRecordModel.setSignImg(signImgUrl);
        }

        if (StringUtils.isEmpty(imgUrl[0]) && StringUtils.isEmpty(imgUrl[1]) && StringUtils.isEmpty(imgUrl[2])){
            ToastUtils.showToast(activity, "提交现场照片");
            return;
        }else {
            addXksRecordModel.setEquImgs(Utils.getImageUrls(imgUrl));
        }
        addXksRecordModel.setRecordItemList(recordItemAdapter.getmDatas());

        List<XksRecordUserModel> xksRecordUserModels = new LinkedList<>();
        for (UserInfoModel userInfoModel:chooseUserList){
            XksRecordUserModel xksRecordUserModel = new XksRecordUserModel();
            xksRecordUserModel.setPhone(userInfoModel.getPhone());
            xksRecordUserModel.setRealName(userInfoModel.getRealName());
            xksRecordUserModel.setUserId(userInfoModel.getId());
            xksRecordUserModels.add(xksRecordUserModel);
        }
        addXksRecordModel.setRecordUserList(xksRecordUserModels);

        HashMap<String, String> url_params = new HashMap<>();
        url_params.put("jsonObject", JSONUtil.beanToJson(addXksRecordModel));
        HttpHelper homeListHttpHelper = new InfoListListHttpHelper(activity, Url.XKS_CHECK_URL, SUBMIT_DETAIL_WHAT, handler, true, url_params);
        homeListHttpHelper.setParseJsonData(new InfoListParseJsonData());
        homeListHttpHelper.getHttpRequest(true);
        include_progress_bar.setVisibility(View.VISIBLE);
    }

    private class InfoListListHttpHelper extends HttpHelper{

        public InfoListListHttpHelper(BaseActivity activity, String url, int what, Handler handler, boolean isPost, HashMap<String, String> params) {
            super(activity, url, what, handler, isPost, params);
        }
    }

    protected class InfoListParseJsonData extends ParseJsonData {

        public InfoListParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            try {
                switch (what) {
                    case SUBMIT_DETAIL_WHAT:
                        break;
                    case RECORD_ITEM_WHAT:
                        LinkedList<XksDutyItemModel> xksRecordItemModels = (LinkedList<XksDutyItemModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(dataStr), XksDutyItemModel.class);
                        if (null != xksRecordItemModels && xksRecordItemModels.size() > 0){
                            bundle.putSerializable("data", xksRecordItemModels);
                            bundle.putBoolean(REQUEST_OK, true);
                        }else {
                            bundle.putBoolean(REQUEST_OK, false);
                            bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
                        }
                        break;
                    case XKS_PERSON_WHAT:
                        LinkedList<UserInfoModel> userInfoModels = (LinkedList<UserInfoModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(dataStr), UserInfoModel.class);
                        if (null != userInfoModels && userInfoModels.size() > 0){
                            bundle.putSerializable("data", userInfoModels);
                            bundle.putBoolean(REQUEST_OK, true);
                        }else {
                            bundle.putBoolean(REQUEST_OK, false);
                            bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
                        }
                        break;
                }
            }catch (Exception e){
                e.printStackTrace();
                bundle.putBoolean(REQUEST_OK, false);
                bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (SUBMIT_DETAIL_WHAT == msg.what && (!bundle.getBoolean(REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE)))){
            include_progress_bar.setVisibility(View.GONE);
            if (msg.what == SUBMIT_DETAIL_WHAT && ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
                PreferencesUtils.putString(activity, ConstData.NEEDREFRESH, "0");
                isSubmitSuccess = true;
                activity.finish();
            }
            if (msg.what == RECORD_ITEM_WHAT){
                include_progress_bar.setVisibility(View.GONE);
            }
            if (msg.what == XKS_PERSON_WHAT){
                include_progress_bar.setVisibility(View.GONE);
            }
            if (msg.what >= ConstData.COMMIT_IMG_WHAT){
                isUpdateImg = false;
                if (null != recordItemAdapter){
                    recordItemAdapter.setUpdateImg(isUpdateImg);
                }
            }
            return;
        }
        switch (msg.what){
            case SUBMIT_DETAIL_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                break;
            case RECORD_ITEM_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                List<XksDutyItemModel> models = (List<XksDutyItemModel>) bundle.getSerializable("data");
                if (null != models && models.size() > 0) {
                    recordItemAdapter.clear();
                    List<XksRecordItemModel> xksRecordItemModels = new ArrayList<>();
                    for (XksDutyItemModel model:models) {
                        XksRecordItemModel xksRecordItemModel = new XksRecordItemModel();
                        xksRecordItemModel.setImgUrls("");
                        xksRecordItemModel.setStatus("1");
                        xksRecordItemModel.setItemId(model.getId());
                        xksRecordItemModel.setItemName(model.getName());
                        xksRecordItemModels.add(xksRecordItemModel);
                    }
                    recordItemAdapter.addDatas(xksRecordItemModels);
                    ll_record_item.setVisibility(View.VISIBLE);
                }else {
                    ll_record_item.setVisibility(View.GONE);
                }
                break;
            case XKS_PERSON_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                List<UserInfoModel> userInfoModels = (List<UserInfoModel>) bundle.getSerializable("data");
                if (null != userInfoModels && userInfoModels.size() > 0) {
                    chooseDialog = getChooseUserListDialog(activity, userInfoModels, chooseUserList, "选择值班人员");
                    chooseDialog.show();
                }else {
                    ToastUtils.showToast(activity, "暂无人员数据");
                }
                break;
        }
        if (msg.what >= ConstData.COMMIT_IMG_WHAT){
            isUpdateImg = false;
            if (null != recordItemAdapter){
                recordItemAdapter.setUpdateImg(isUpdateImg);
            }
            include_progress_bar.setVisibility(View.GONE);
            int imgWhat = msg.what - ConstData.COMMIT_IMG_WHAT;
            switch (imgWhat){
                case ConstData.PHOTO_DEVICE_LIST_1:
                    imgUrl[0] = bundle.getString("fileName");
                    updateImageView(ConstData.IMG_TYPE_STATUS);
                    break;
                case ConstData.PHOTO_DEVICE_LIST_2:
                    imgUrl[1] = bundle.getString("fileName");
                    updateImageView(ConstData.IMG_TYPE_STATUS);
                    break;
                case ConstData.PHOTO_DEVICE_LIST_3:
                    imgUrl[2] = bundle.getString("fileName");
                    updateImageView(ConstData.IMG_TYPE_STATUS);
                    break;
                case ConstData.PHOTO_USER_SIGN:
                    signImgUrl = bundle.getString("fileName");
                    GildeUtils.loadImageCorner(activity, iv_up_sign, signImgUrl);
                    iv_up_sign_delete.setVisibility(View.VISIBLE);
                    break;
                default:
                    String strUrl = bundle.getString("fileName");
                    if (null != recordItemAdapter) {
                        List<XksRecordItemModel> xksRecordItemModels = recordItemAdapter.getmDatas();
                        if (imgWhat >= 3000) {
                            int position = imgWhat - 3000;
                            String[] imgUrls = xksRecordItemModels.get(position).getImgUrls().split(",");
                            xksRecordItemModels.get(position).setImgUrls(Utils.getImageUrls(imgUrls) + "," + strUrl);
                        } else if (imgWhat >= 2000) {
                            int position = imgWhat - 2000;
                            String[] imgUrls = xksRecordItemModels.get(position).getImgUrls().split(",");
                            xksRecordItemModels.get(position).setImgUrls(Utils.getImageUrls(imgUrls) + "," + strUrl);
                        } else if (imgWhat >= 1000) {
                            int position = imgWhat - 1000;
                            String[] imgUrls = xksRecordItemModels.get(position).getImgUrls().split(",");
                            imgUrls[0] = strUrl;
                            xksRecordItemModels.get(position).setImgUrls(Utils.getImageUrls(imgUrls));
                        }
                        recordItemAdapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    }

    /**
     * 选择用户列表
     * @param context
     * @param allList
     * @param title
     * @return
     */
    private Dialog getChooseUserListDialog(Context context, List<UserInfoModel> allList, List<UserInfoModel> currentChooseList, String title){
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_list, null);
        TextView tv_title = view.findViewById(R.id.tv_title);
        View v_line = view.findViewById(R.id.v_line);

        RecyclerView rlv_list = view.findViewById(R.id.rlv_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlv_list.setLayoutManager(linearLayoutManager);
        ChooseUserAdapter chooseUserAdapter = new ChooseUserAdapter(context, currentChooseList);
        chooseUserAdapter.addDatas(allList);
        rlv_list.setAdapter(chooseUserAdapter);
        chooseUserAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<UserInfoModel>() {
            @Override
            public void onItemClick(View v, int position, UserInfoModel data) {
                if (data.getId().equals(UserInfoModel.getUserInfo(context).getId())){
                    ToastUtils.showToast(activity, "当前用户不可取消");
                    return;
                }
                UserInfoModel selectModel = null;
                boolean isSelect = false;
                for (int i = 0; i < currentChooseList.size(); i++){
                    if (data.getId().equals(currentChooseList.get(i).getId())){
                        isSelect = true;
                        selectModel = currentChooseList.get(i);
                        break;
                    }
                }
                if (isSelect){
                    currentChooseList.remove(selectModel);
                }else {
                    currentChooseList.add(data);
                }
                chooseUserAdapter.setCurrentChooseList(currentChooseList);

                updateChooseData();
            }
        });

        if (StringUtils.isEmpty(title)){
            tv_title.setVisibility(View.GONE);
            v_line.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rlv_list.getLayoutParams();
            layoutParams.topMargin = Utils.dip2px(context, 15);
        }else {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
            v_line.setVisibility(View.VISIBLE);
        }

        final Dialog dialog = new Dialog(context, R.style.loading_dialog_tran);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ScreenUtils.getScreenHeight(context) * 2 / 3));
        dialog.setCanceledOnTouchOutside(true);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.closeDialog(dialog);
            }
        });
        return dialog;
    }

    /**
     * 跳转拍照和相册
     * @param photoWhat
     * @param isPlayPhoto
     */
    public void addImageOrPlayPhoto(int photoWhat, boolean isPlayPhoto){
        if (activity instanceof ShowFragActivity) {
            if (isPlayPhoto) {
                ((ShowFragActivity)activity).skipToPlayPhoto(photoWhat);
            } else {
                ((ShowFragActivity)activity).initSubmitPicView(photoWhat);
            }
        }
    }

    @Override
    public void imgSuccessAction(File file, String img, int photoWhat) {
        super.imgSuccessAction(file, img, photoWhat);
        commitPic(file, photoWhat, img);
    }

    /**
     * 提交图片
     * @param file
     * @param what
     */
    private void commitPic(final File file, final int what, final String img){
        isUpdateImg = true;
        if (null != recordItemAdapter){
            recordItemAdapter.setUpdateImg(isUpdateImg);
        }
        include_progress_bar.setVisibility(View.VISIBLE);
        MinioUtils.upLoadFile(activity, what, file, handler);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!isSubmitSuccess){
            if (!StringUtils.isEmpty(signImgUrl)){
                MinioUtils.removeFile(activity, signImgUrl, handler);
            }
            for (int i = 0; i < imgUrl.length; i++){
                if (!StringUtils.isEmpty(imgUrl[i])){
                    MinioUtils.removeFile(activity, imgUrl[i], handler);
                }
            }
            if (null != recordItemAdapter) {
                List<XksRecordItemModel> xksRecordItemModels = recordItemAdapter.getmDatas();
                if (null != xksRecordItemModels) {
                    for (XksRecordItemModel xksRecordItemModel : xksRecordItemModels) {
                        String[] imgUrls = xksRecordItemModel.getImgUrls().split(",");
                        for (int i = 0; i < imgUrls.length; i++){
                            if (!StringUtils.isEmpty(imgUrls[i])){
                                MinioUtils.removeFile(activity, imgUrls[i], handler);
                            }
                        }
                    }
                }
            }
        }

        DialogUtils.closeDialog(chooseDialog);
    }

    /**
     * 把链接数组拼成String, 逗号分隔
     * @param imgUrl
     * @return
     */
    private String getImgUrl(String[] imgUrl){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < imgUrl.length; i++){
            if (!StringUtils.isEmpty(imgUrl[i])){
                if (StringUtils.isEmpty(stringBuilder.toString())){
                    stringBuilder.append(imgUrl[i]);
                }else {
                    stringBuilder.append("," + imgUrl[i]);
                }
            }
        }
        return stringBuilder.toString();
    }

    private void updateImageView(String imgViewType){
        RelativeLayout rl_device_photo_1;
        ImageView iv_device_photo_1;
        ImageView iv_photo_delete_1;
        RelativeLayout rl_device_photo_2;
        ImageView iv_device_photo_2;
        ImageView iv_photo_delete_2;
        RelativeLayout rl_device_photo_3;
        ImageView iv_device_photo_3;
        ImageView iv_photo_delete_3;
        String[] imgUrl;
        switch (imgViewType){
            default:
                rl_device_photo_1 = this.rl_device_photo_1;
                iv_device_photo_1 = this.iv_device_photo_1;
                iv_photo_delete_1 = this.iv_photo_delete_1;
                rl_device_photo_2 = this.rl_device_photo_2;
                iv_device_photo_2 = this.iv_device_photo_2;
                iv_photo_delete_2 = this.iv_photo_delete_2;
                rl_device_photo_3 = this.rl_device_photo_3;
                iv_device_photo_3 = this.iv_device_photo_3;
                iv_photo_delete_3 = this.iv_photo_delete_3;
                imgUrl = this.imgUrl;
                break;
        }
        updateImageUrl(imgUrl);
        int size = 0;
        for (int i = 0; i < imgUrl.length; i++){
            if (!StringUtils.isEmpty(imgUrl[i])){
                size++;
            }
        }
        switch (size){
            case 3:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);

                rl_device_photo_3.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_3, imgUrl[2]);
                iv_photo_delete_3.setVisibility(View.VISIBLE);
                break;
            case 2:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_2, imgUrl[1]);
                iv_photo_delete_2.setVisibility(View.VISIBLE);

                rl_device_photo_3.setVisibility(View.VISIBLE);
                iv_device_photo_3.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_3.setVisibility(View.INVISIBLE);
                break;
            case 1:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                GildeUtils.loadImageCorner(activity, iv_device_photo_1, imgUrl[0]);
                iv_photo_delete_1.setVisibility(View.VISIBLE);

                rl_device_photo_2.setVisibility(View.VISIBLE);
                iv_device_photo_2.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
            case 0:
                rl_device_photo_1.setVisibility(View.VISIBLE);
                iv_device_photo_1.setImageResource(R.mipmap.icon_duty_detail_photo_big);
                iv_photo_delete_1.setVisibility(View.INVISIBLE);

                rl_device_photo_2.setVisibility(View.INVISIBLE);

                rl_device_photo_3.setVisibility(View.INVISIBLE);
                break;
        }
    }

    /**
     * 调整图片链接顺序
     */
    private void updateImageUrl(String[] imgUrl){
        if (null != imgUrl && imgUrl.length > 0){
            for (int i = 0; i < imgUrl.length; i++){
                if (StringUtils.isEmpty(imgUrl[i])){
                    for (int j = i + 1; j < imgUrl.length; j++){
                        if (!StringUtils.isEmpty(imgUrl[j])){
                            imgUrl[i] = imgUrl[j];
                            imgUrl[j] = "";
                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * 删除用户
     * @param userInfoModel
     */
    @Override
    public void deletePerson(UserInfoModel userInfoModel) {
        chooseUserList.remove(userInfoModel);
        updateChooseData();
    }

    /**
     * 更新
     */
    private void updateChooseData(){
        xksPersonAdapter.clear();
        if (chooseUserList.size() == 1) {
            rlv_person_list.setVisibility(View.GONE);
        }else {
            xksPersonAdapter.addDatas(chooseUserList.subList(1, chooseUserList.size()));
            rlv_person_list.setVisibility(View.VISIBLE);
        }
    }
}
